import { _decorator, Component, director, Label, Node, Sprite } from 'cc';
import NodeUtil from '../../Framework/Utils/NodeUtil';
import { Panel, UIManager } from '../../Framework/Managers/UIManager';
import { PanelBase } from '../../Framework/UI/PanelBase';
import { GameData } from 'db://assets/MigaWorlds/Scripts/GameData';
const { ccclass, property, menu } = _decorator;

@ccclass('LoadingPanel')
@menu("Framework/UI组件/Panel_Loading")
export default class LoadingPanel extends PanelBase {
    Loading: Node = null;
    MapLabel: Label | null = null;
    LoadingLabel: Label | null = null;
    LoadingFG: Sprite | null = null;

    isLoading = false;

    protected onLoad(): void {
        this.Loading = NodeUtil.GetNode("Loading", this.node);
        this.LoadingLabel = NodeUtil.GetComponent("LoadingLabel", this.node, Label);
        this.MapLabel = NodeUtil.GetComponent("MapLabel", this.node, Label);
        this.LoadingFG = NodeUtil.GetComponent("LoadingFG", this.node, Sprite);
        GameData.eventTarget.on("DataLoad", this.loadScene, this);
    }

    loadScene(sceneName = "GameScene", bundleName = null) {
        if (sceneName == "GameScene" && GameData.DataLoad < 3 || !this.node.active || this.isLoading) return;
        this.isLoading = true;
        director.loadScene(sceneName, () => {
            UIManager.HidePanel(Panel.LoadingPanel);
        });
        this.LoadingLabel.string = `加载中...`;



        this.Loading.active = false;
        director.preloadScene(sceneName, (completedCount: number, totalCount: number, item: any) => {
            this.LoadingFG.fillRange = this.LoadingFG.fillRange > completedCount / totalCount ? this.LoadingFG.fillRange : completedCount / totalCount;
            this.LoadingLabel.string = `正在加载：${Math.ceil(completedCount / totalCount * 100)}%`;
        }, () => {
        });
    }

    //**一个参数为直接跳转到该场景，两个参数第一个参数为GameData，第二个为跳转到GameData的默认分包里的场景 */
    Show(...args: any[]) {
        this.isLoading = false;
        this.node.active = true;
        this.LoadingFG.fillRange = 0;
        this.LoadingLabel.string = `正在加载：${0}%`;

        this.Loading.active = args.length >= 2;

        // if (args.length == 1) {
        this.loadScene(args[0]);
        // }

        // if (args.length >= 2) {
        //     if (!(args[0] instanceof GameData)) {
        //         console.error(`第一个参数类型错误，应该传入GameData。`, args[0]);
        //         return;
        //     }

        //     let data = args[0] as GameData;
        //     let scene = args[1];

        //     if (data.Bundles) {
        //         BundleManager.LoadBundles(data.Bundles, () => {
        //             this.Loading.active = false;
        //             loadScene(scene, data.DefaultBundle);
        //         }, () => {
        //             UIManager.ShowTip("网络异常，请稍后重试");
        //             UIManager.HidePanel(Panel.LoadingPanel);
        //         });
        //     } else {
        //         this.Loading.active = false;
        //         loadScene(scene, data.DefaultBundle);
        //     }
        // }

    }
}